package com.pfzy.eventbusdemo

import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
import androidx.annotation.Nullable
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MyService : Service() {
    override fun onCreate() {
        super.onCreate()
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    @Nullable
    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    /**
     * POSTING线程模型：在哪个线程发布事件，就在哪个线程执行onPostingEvent方法
     */
    @Subscribe(threadMode = ThreadMode.POSTING, priority = 1)
    fun onPostingEvent(event: MyEvent?) {
        Log.d(TAG, "onPostingEvent:" + Thread.currentThread().name)
    }

    companion object {
        private const val TAG = "MyService"
    }
}